package com.qiu.demo.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiu.demo.entity.ArticleCate;
import com.qiu.demo.entity.User;
import com.qiu.demo.entity.dto.ArticleCateDto;
import com.qiu.demo.entity.vo.ArticleCateVo;
import com.qiu.demo.service.ArticleCateService;
import com.qiu.demo.util.JsonResult;
import com.qiu.demo.util.TokenUtil;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;

@RestController
@RequestMapping("/cate")
@CrossOrigin
public class ArticleCateController {

    private final ApiController apiController;

    @Resource
    private TokenUtil tokenUtil;

    @Resource
    private ArticleCateService articleCateService;

    ArticleCateController(ApiController apiController) {
        this.apiController = apiController;
    }

    @GetMapping("list")
    public JsonResult list(HttpServletRequest request) {
        Map userData = tokenUtil.parseToken(request.getHeader("Authorization"));
        String userId = userData.get("userId").toString();

        List<ArticleCate> aCates = articleCateService
                .list(new LambdaQueryWrapper<ArticleCate>().eq(ArticleCate::getUserId, userId));

        List<ArticleCateVo> aCateVos = aCates.stream()
                .map(cate -> {
                    ArticleCateVo articleCateVo = new ArticleCateVo();
                    articleCateVo.setId(cate.getCateId().toString());
                    articleCateVo.setCate_name(cate.getCateName());
                    articleCateVo.setCate_alias(cate.getCateAlias());
                    return articleCateVo;
                })
                .collect(Collectors.toList());

        return JsonResult.success().data("list", aCateVos);
    }

    @PostMapping("add")
    public JsonResult add(@RequestBody ArticleCateDto articleCateDto, HttpServletRequest request) {
        Map userData = tokenUtil.parseToken(request.getHeader("Authorization"));
        String userId = userData.get("userId").toString();

        ArticleCate articleCate = new ArticleCate();
        articleCate.setCateName(articleCateDto.getCate_name());
        articleCate.setCateAlias(articleCateDto.getCate_alias());
        articleCate.setUserId(userId);

        articleCateService.save(articleCate);

        return JsonResult.success();
    }

    @PutMapping("info")
    public JsonResult updateInfo(@RequestBody ArticleCateDto articleCateDto, HttpServletRequest request) {
        Map userData = tokenUtil.parseToken(request.getHeader("Authorization"));
        String userId = userData.get("userId").toString();

        ArticleCate articleCate = new ArticleCate();
        articleCate.setCateName(articleCateDto.getCate_name());
        articleCate.setCateAlias(articleCateDto.getCate_alias());

        articleCateService.update(articleCate, new LambdaQueryWrapper<ArticleCate>()
                .eq(ArticleCate::getCateId, articleCateDto.getId()).eq(ArticleCate::getUserId, userId));

        return JsonResult.success();
    }

    @DeleteMapping("del")
    public JsonResult del(String id, HttpServletRequest request) {
        Map userData = tokenUtil.parseToken(request.getHeader("Authorization"));
        String userId = userData.get("userId").toString();

        articleCateService.remove(new LambdaQueryWrapper<ArticleCate>()
                .eq(ArticleCate::getCateId, id).eq(ArticleCate::getUserId, userId));

        return JsonResult.success();
    }
}
